package services

import (
	"fmt"
	"github.com/jinzhu/copier"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/timehelper"
	utils2 "longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql/db_main"
	"longmen/server/pkg/db/mysql/models/models_main"
)

func (s *Service) GetActivityProfileList(page, size int) (*api_models.GetActivityProfileListResp, error) {

	var (
		err                 error
		activityList        []*models_main.Activity
		timeStampNow        = timehelper.GetTimeStamp(1)
		ongoingActivityList = make([]*api_models.ActivityProfile, 0)
		overActivityList    = make([]*api_models.ActivityProfile, 0)
	)
	return &api_models.GetActivityProfileListResp{
		OngoingActivityList: ongoingActivityList,
		OverActivityList:    overActivityList,
	}, nil

	activityList, err = db_main.GetActivityListByParam(map[string]interface{}{}, "weight DESC", page, size)
	if err != nil {
		return nil, err
	}

	for _, activity := range activityList {
		dest := &api_models.ActivityProfile{}
		err = copier.Copy(dest, activity)
		if err != nil {
			return nil, fmt.Errorf("%s%s", utils2.Convert2(), err.Error())
		}

		dest.StartTime = activity.StartTime.Unix()
		dest.EndTime = activity.EndTime.Unix()
		if timeStampNow > activity.EndTime.Unix() {
			overActivityList = append(overActivityList, dest)
		} else if timeStampNow <= activity.EndTime.Unix() && timeStampNow >= activity.StartTime.Unix() {
			ongoingActivityList = append(ongoingActivityList, dest)
		}
	}

	return &api_models.GetActivityProfileListResp{
		OngoingActivityList: ongoingActivityList,
		OverActivityList:    overActivityList,
	}, nil
}

func (s *Service) GetActivityById(id, uid int) (*api_models.GetActivityByIdResp, error) {
	activity, err := db_main.GetActivityById(id)
	if err != nil {
		return nil, err
	}

	reward, err := s.GetActivityRewardById(id)
	if err != nil {
		return nil, err
	}

	o := &api_models.GetActivityByIdResp{}
	err = copier.Copy(o, reward)
	if err != nil {
		return nil, fmt.Errorf("%s%s", utils2.Convert2(), err.Error())
	}

	err = copier.Copy(o, activity)
	if err != nil {
		return nil, fmt.Errorf("%s%s", utils2.Convert2(), err.Error())
	}

	o.StartTime = activity.StartTime.Unix()
	o.EndTime = activity.EndTime.Unix()
	return o, nil
}
